package www.sharetime.weshare.base;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import www.sharetime.weshare.event.NetWorkEvent;


public abstract class BaseOhterActivity extends Activity implements
        BaseInterface {

    protected RequestManager mImageLoader;
    private Unbinder unbinder;
    protected LayoutInflater mInflater;

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (unbinder != null){
            unbinder.unbind();
        }

        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void initData() {

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCallBack(NetWorkEvent netWorkEvent) {
        if (netWorkEvent != null && netWorkEvent.getType() == NetWorkEvent.UNAVAILABLE) {
            //TODO: 当网络状态变化时，做相应动作
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onBeforeSetContentLayout();

        if (initBundle(getIntent().getExtras())) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            if (getLayoutId() != 0) {
                setContentView(getLayoutId());
            }

            initWindow();

            ButterKnife.bind(this);
            mInflater = getLayoutInflater();

            initView(savedInstanceState);
            initData();
        } else {
            finish();
        }
    }

    protected void initWindow() {
    }


    protected boolean initBundle(Bundle bundle) {
        return true;
    }


    protected void onBeforeSetContentLayout() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    protected abstract int getLayoutId();

    protected View inflateView(int resId) {
        return mInflater.inflate(resId, null);
    }

    protected void init(Bundle savedInstanceState) {
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            backKeyDown(keyCode, event);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    /**
     * 如果需要监听别的键盘事件，复写此方法
     *
     * @param keyCode
     * @param event
     */
    public void backKeyDown(int keyCode, KeyEvent event) {

    }

    public synchronized RequestManager getImageLoader() {
        if (mImageLoader == null)
            mImageLoader = Glide.with(this);
        return mImageLoader;
    }
}
